Skip to content

fix(sql): store and read cached SQL results only as Parquet - #157

Open
tkislan wants to merge 1 commit into
mainfrom
tk/sql-cache-parquet-only
Open

tkislan wants to merge 1 commit into
mainfrom
tk/sql-cache-parquet-only

Conversation

@tkislan

@tkislan tkislan commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Summary

The SQL result cache fell back to pickle in both directions. upload_sql_cache uploaded a pickle when a result couldn't be written as Parquet, and _try_read_cache loaded any cache object that wasn't Parquet with pandas.read_pickle. Unpickling downloaded bytes runs whatever code they contain, so this PR makes the cache Parquet-only:

  • Upload: when to_parquet raises ArrowNotImplementedError, ArrowInvalid or OverflowError, the toolkit logs a warning with sql_caching_cause: "result_not_parquet_serializable" and doesn't upload. The notebook still gets the result. Other upload failures are logged as before (failed_to_upload_to_cache).
  • Read: a cache hit whose object isn't Parquet (ArrowInvalid) is ignored. The toolkit logs a warning with sql_caching_cause: "cache_object_not_parquet", doesn't emit read_from_cache_success, and runs the query against the warehouse. Other download errors are still logged as failed_to_download_from_cache.
  • pyarrow: the floor on Python 3.10 and 3.11 outside macOS goes from 10.0.0 / 13.0.0 to 14.0.1, the release that fixes CVE-2023-47248, in which pyarrow could unpickle data while reading Parquet. The macOS lines and Python 3.12+ were already above it.

get_sql_cache, the webapp request, cache keys and the presigned-URL logging are unchanged.

Behavior changes

  • Results Parquet can't hold aren't cached. For example, integers outside the int64 range. They used to be cached as pickles. These queries now run every time.
  • Existing pickle cache objects stop being used. A hit on one now acts as a miss without an upload URL, so the query runs uncached until the webapp's cache entry expires or is rewritten. This window is bounded by the project's max cache age.
  • Both new log entries are warnings, so they go to the kernel log and are not reported to the webapp, which receives only ERROR records.

Lock

Only content-hash changes in poetry.lock. The locked pyarrow versions (16.1.0 for Python < 3.12, 24.0.0 for 3.12+) already satisfy >=14.0.1. A full poetry lock with Poetry 2.2.0 rewrites about 1,180 unrelated lines, even on unchanged main, so this follows #143, #145 and #146. Those PRs also kept only the relevant changes and the new hash. poetry check --lock passes, and poetry install --dry-run installs nothing new.

Tests

  • Replaced:
    • test_fallback_to_pickle_format with test_pickle_cache_object_is_cache_miss. A real pickle file is served as the cache hit, with the real read_parquet / read_pickle.
    • test_overflow_error_falls_back_to_pickle with test_result_parquet_cannot_hold_is_not_uploaded. It is parameterized over one input per caught exception: an int beyond int64 (OverflowError), a mixed int/str column (ArrowInvalid) and a complex column (ArrowNotImplementedError).
  • Removed:
    • test_pickle_fallback_truncates_partial_parquet_bytes, because the behavior it covered is gone.
    • test_read_from_cache_error_doesnt_raise. It never reached the readers, because its unpatched webapp request failed first.
  • Sharpened: test_failed_to_download_from_cache now asserts that the failed_to_download_from_cache error is logged. Without that check, widening the read's except ArrowInvalid to except Exception passed every test.
  • Fail before, pass after: each new test fails against main's sql_caching.py for the intended reason. Mutating the new code is caught in each case: re-adding the pickle read, removing the early return before the PUT, dropping any of the three exception types, logging at a different level, and dropping error_type.
  • Runs:
    • tests/unit/test_sql_caching.py: 36 passed.
    • Full unit suite: 1474 passed, 4 skipped (Python 3.13, pyarrow 24.0.0).
    • Outside the suite, on both locked pyarrow versions (16.1.0 and 24.0.0), each test input raises the expected exception type. A pickle served over HTTP, which is how presigned URLs are read, raises ArrowInvalid from read_parquet.

Docs

docs/sql-query-caching.md in deepnote/deepnote lists when caching isn't applied. It needs a new line saying results that can't be stored as Parquet aren't cached: deepnote/deepnote#693, to merge after this PR is released.

The SQL result cache fell back to pickle in both directions: results
that Parquet can't hold were uploaded as pickles, and cache objects
that weren't Parquet were loaded with pandas.read_pickle. Unpickling
downloaded bytes runs whatever code they contain, so the cache now
holds Parquet only.

- upload_sql_cache skips the upload and logs a warning
  (result_not_parquet_serializable) when to_parquet fails.
- A cache hit whose object isn't Parquet is ignored with a warning
  (cache_object_not_parquet), and the query runs against the warehouse.
- pyarrow >= 14.0.1 on Python 3.10 and 3.11 outside macOS, the release
  that fixes CVE-2023-47248. Only poetry.lock's content-hash changes:
  the locked pyarrow versions already satisfy the floor.
@coderabbitai

coderabbitai Bot commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

SQL cache uploads now skip results that raise specified errors during Parquet serialization. Cache reads treat invalid Parquet objects as misses instead of attempting pickle deserialization. The PyArrow minimum versions increase for non-macOS Python 3.10 and 3.11. Tests cover these behaviors and failed-download logging.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Suggested reviewers: m1so

Merge Risk: Minimal · up to 1503b · assessed changes only

Path filters excluded changed files. This rating applies only to the assessed changes.

Pre-merge checks: Passed 4 · Inconclusive 1

Failed Failed checks: 1 inconclusive

Check name Status Explanation Resolution
Updates Docs Inconclusive The PR changes SQL cache behavior. It makes the cache Parquet-only, skips uploads for results Parquet cannot hold, and treats pickle cache objects as misses. The changed files in this repo are `deepno… Remind the author to update the primary docs in deepnote/deepnote. Specifically, update docs/sql-query-caching.md to state that results Parquet cannot store are not cached. Also document that existing pickle cache objects act as misses un…
Passed Passed checks: 4 passed

Check name Status Explanation
Title check Passed The title names the main change: the SQL result cache now stores and reads results only as Parquet. The changeset removes the pickle fallback on upload and read in sql_caching.py, and the tests reflec…
Description check Passed The description explains the change in detail. It covers the removal of the pickle fallback on upload and read, the new warning log entries, the pyarrow floor increase to 14.0.1, the lockfile change, …
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: the SQL result cache now stores and reads results only as Parquet. The changeset removes the pickle fallback on upload and read in sql_caching.py, and the tests reflect that. The title is specific and accurate.

Full details: Description check

Explanation

The description explains the change in detail. It covers the removal of the pickle fallback on upload and read, the new warning log entries, the pyarrow floor increase to 14.0.1, the lockfile change, test updates, and expected behavior changes. It matches the sql_caching.py, pyproject.toml, and test changes.

Full details: Updates Docs

Explanation

The PR changes SQL cache behavior. It makes the cache Parquet-only, skips uploads for results Parquet cannot hold, and treats pickle cache objects as misses. The changed files in this repo are deepnote_toolkit/sql/sql_caching.py, poetry.lock, pyproject.toml, and tests/unit/test_sql_caching.py. No docs file in this repo changes. A search of the repo's Markdown files finds no SQL caching documentation to update. The primary docs are in deepnote/deepnote, which I cannot see. The PR description says the docs update is deferred to deepnote/deepnote#693, to merge after this PR is released. That deferral is not a docs update in the reviewed scope. The check directs a reminder when the other repo is not visible. The roadmap in deepnote-internal is also not visible. I cannot confirm the docs are updated, so I cannot pass the check.

Resolution

Remind the author to update the primary docs in deepnote/deepnote. Specifically, update docs/sql-query-caching.md to state that results Parquet cannot store are not cached. Also document that existing pickle cache objects act as misses until the cache entry expires or is rewritten. Link deepnote/deepnote#693 to this PR and confirm it merges with or before the release. Also check the roadmap in deepnote/deepnote-internal, which I could not access, and update it if it tracks this work.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Essentials
  • Run ID: 72424995-0372-40c8-ab21-c86ec68e4f5a
Commits

Reviewing files that changed from the base of the PR and between 39537e8 and 1503bb5.

Files ignored due to path filters: 1

  • poetry.lock is excluded by !**/*.lock
Files selected for processing: 3

  • deepnote_toolkit/sql/sql_caching.py
  • pyproject.toml
  • tests/unit/test_sql_caching.py

Included review availability: This review used your included allowance. 2 included reviews remain after this review. Your included PR review attempts over the past 7 days set your current allowance at 3 reviews per hour.

@github-actions

github-actions Bot commented Dec 3, 2026

Copy link
Copy Markdown

📦 Python package built successfully!

  • Version: 2.9.1.dev7+0cf9c2e
  • Wheel: deepnote_toolkit-2.9.1.dev7+0cf9c2e-py3-none-any.whl
  • Install:
    pip install "deepnote-toolkit @ https://deepnote-staging-runtime-artifactory.s3.amazonaws.com/deepnote-toolkit-packages/2.9.1.dev7%2B0cf9c2e/deepnote_toolkit-2.9.1.dev7%2B0cf9c2e-py3-none-any.whl"

@codecov

codecov Bot commented Dec 3, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 78.56%. Comparing base (39537e8) to head (1503bb5).
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #157      +/-   ##
==========================================
- Coverage   78.57%   78.56%   -0.01%     
==========================================
  Files         121      121              
  Lines        6935     6932       -3     
  Branches     1009     1009              
==========================================
- Hits         5449     5446       -3     
- Misses       1179     1181       +2     
+ Partials      307      305       -2     
Flag Coverage Δ
combined 78.56% <100.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

@deepnote-bot

Copy link
Copy Markdown

🚀 Review App Deployment Started

📝 Description 🌐 Link / Info
🌍 Review application ra-157
🔑 Sign-in URL Click to sign-in
📊 Application logs View logs
🔄 Actions Click to redeploy
🚀 ArgoCD deployment View deployment
⏰ Last deployed 2026-12-03 20:37:52 (UTC)
📜 Deployed commit 079bed5d519a037175cedea71814ed6701b6b2e3
🛠️ Toolkit version 0cf9c2e

@tkislan
tkislan marked this pull request as ready for review December 4, 2026 16:36
@tkislan
tkislan requested a review from a team as a code owner December 4, 2026 16:36
@tkislan
tkislan requested a review from mfranczel December 4, 2026 16:36

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants